The main loop for the recursive version is almost identical to the iterative one.
- The function calls are now directed to your new recursive implementations.
- For successor and predecessor, you only need to pass the root and the key. The optional candidate parameter will use its default value of None for the initial call.
- The logic for parsing input and printing output remains the same, showing how the implementation can be swapped without changing program flow.
# --- Main Program Logic ---
root = None
num_operations = int(input())
for _ in range(num_operations):
line = input().strip().split()
op, key = line[0], int(line[1])
if op == "ins":
root = __________(root, key)
elif op == "find":
result = __________(root, key)
print(__________)
elif op == "succ":
result = __________(root, key)
print(__________)
elif op == "pred":
result = __________(root, key)
print(__________)
Copied!